package com.yiwei.jmx.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.ModifyPhoneBean;
import com.yiwei.jmx.api.bean.ModifyPhoneCheckCodeBean;
import com.yiwei.jmx.api.bean.ModifyPhoneSendCodeBean;
import com.yiwei.jmx.api.bean.SmsSendBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.PreferenceUtils;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.view.FlowViewHorizontal;
import com.yiwei.jmx.view.SelectMenuTipsDilaog;
import com.yiwei.jmx.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class ChangePhoneOneActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.step_view)
    FlowViewHorizontal stepView;
    @BindView(R.id.edit_user)
    EditText editUser;
    @BindView(R.id.btn_account_clear)
    ImageView btnAccountClear;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.btn_send_code)
    TextView btnSendCode;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Unbinder unbiner;
    private static final int TIME = 60 * 1000;  //验证码倒计时时间
    private CountDownTimer timer = new CountDownTimer(TIME, 1000) {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onTick(long millisUntilFinished) {
            try {
                btnSendCode.setEnabled(false);
                btnSendCode.setText((millisUntilFinished / 1000) + "s重新获取");
//                btnSendCode.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray));
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFinish() {
            resetBtn();
        }
    };


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void resetBtn() {
        if (timer != null) timer.cancel();
        btnSendCode.setEnabled(true);
        btnSendCode.setText("重新发送");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone_sec);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        stepView.setProgress(1, 2, new String[]{"验证手机号", "修改手机号"}, null);
        txtTitle.setText("人工认证");
    }

    @OnTextChanged({R.id.edit_user, R.id.edit_code})
    void onTextChange(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editUser)) && !TextUtils.isEmpty(UI.toString(editCode))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        unbiner.unbind();
        unbiner = null;
    }


    public void getSmsSend(String mobile) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        ModifyPhoneSendCodeBean bean = new ModifyPhoneSendCodeBean();
        bean.mobile = mobile;
        Api.api_service.getModifyphoneSendcode(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    timer.start();
                });
    }

    public void getModifyphoneCheckcode(String mobile, String verify_code) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        ModifyPhoneCheckCodeBean bean = new ModifyPhoneCheckCodeBean();
        bean.mobile = mobile;
        bean.verify_code = verify_code;
        Api.api_service.getModifyphoneCheckcode(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "验证成功");
                    Intent msgIntent = new Intent(this, ChangePhoneSecActivity.class);
                    msgIntent.putExtra("is_two", true);
                    msgIntent.putExtra("token", result.token);
                    startActivity(msgIntent);
                    overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                    finish();
                });
    }


    @OnClick({R.id.btn_back, R.id.btn_account_clear, R.id.btn_send_code, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_account_clear:
                editUser.setText("");
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_send_code:
                if (UI.toString(editUser).length() != 11) {
                    ToastUtil.makeText(this, "请输入正确的手机号");
                    return;
                }
                getSmsSend(UI.toString(editUser));
                break;
            case R.id.btn_submit:
                if (editUser.getText().toString().length() < 11) {
                    ToastUtil.makeText(this, "请输入11位手机号").show();
                    return;
                }
                if (editCode.getText().toString().length() < 6) {
                    ToastUtil.makeText(this, "请输入完整验证码").show();
                    return;
                }
                getModifyphoneCheckcode(UI.toString(editUser), UI.toString(editCode));
                break;
        }
    }
}
